<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>店员信息</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="apple-mobile-web-app-status-bar-style" content="black">
	<meta name="apple-mobile-web-app-capable" content="yes">
	<meta name="format-detection" content="telephone=no">
	<link rel="stylesheet" href="plugins/layui/css/layui.css" media="all" />
	<link rel="stylesheet" href="//at.alicdn.com/t/font_tnyc012u2rlwstt9.css" media="all" />
	<link rel="stylesheet" href="../../css/news.css" media="all" />
</head>
<body class="childrenBody">
	<blockquote class="layui-elem-quote news_search">
		<form class="layui-form">
		<div class="layui-inline">
		    <div class="layui-input-inline">
		    	<input type="text" value="" placeholder="请输入用户名" name="username" class="layui-input search_input">
		    </div>

			<a class="layui-btn search_btn" lay-submit=""
			   lay-filter="queryUser">查询</a>
		</div>
 		<div class="layui-inline">
			<a class="layui-btn layui-btn-normal clerksAdd_btn">添加店员</a>
		</div>
		</form>
	</blockquote>
	<div class="layui-form news_list">
	  	<table class="layui-table" id="userList" lay-filter="clerks">

		</table>
	</div>
	<div id="page"></div>
	<script type="text/javascript" src="plugins/layui/layui.js"></script>
	
		    <script type="text/html" id="barDemo">
	  <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
	    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
	  </script>
	<script type="text/javascript">
	layui.use(['table','form','jquery'], function(){
	    var table = layui.table,
		 form = layui.form,
		  $ = layui.jquery;
	    //方法级渲染
	    var tableIns=table.render({
	        elem: '#userList'
	        ,url: '/clerks'
	        ,cols: [[
	            {checkbox: true, fixed: true}
	            ,{field:'id', title: 'ID', width:'10%', sort: true, fixed: true}
	            ,{field:'username', title: '账号',  width:'20%'}
	            ,{field:'name', title: '姓名',  width:'10%'}
	            ,{field:'phone', title: '手机',  width:'10%'}
	            ,{field:'merchantName', title: '所属门店',  width:'20%'}
	            ,{field:'roleName', title: '角色',  width:'10%'}
	            ,{title: '操作',align:'center', toolbar: '#barDemo'} //这里的toolbar值是模板元素的选择器
	            
	            /*  ,{field:'date', title: '日期', width:180}
	            ,{field:'datatype', title: '日期类型', width:100}*/

	        ]]
	        ,id: 'testReload'
	        ,page: true
//	        ,height: 600
	    });


        form.on("submit(queryUser)", function(data) {
            var username=data.field.username;
            //表格重新加载
            tableIns.reload({
                where : {
                    username:username
                }
            });
            return false;
        });
	    form.on('switch(ifKeyDemo)', function(obj){                                   
	        var url='/users/changeStatus';
	        var userId=this.value;
	        var status=this.checked ? '1' : '0'; 
	        $.ajax({  
	            type: "POST",  
	            url:url,  
	            data:{id:userId,status:status},// 序列化表单值  
	            success: function(data) {  
	            	if(data.code==2000){
	          			top.layer.msg("修改成功");
	            	}else{
	            		top.layer.msg("修改失败");
	            	}
	            
	            } ,
	 		
		 		 error: function(res) {
		             alert("请求错误");
		         } 
	        });
	    });
	    
		
			$(".clerksAdd_btn").click(function(){
				var index = layui.layer.open({
					title : "添加店员信息",
					type : 2,
					area:['500px','500px'],
					content : "clerk/clerksAdd.html"
				})			
			})
		
	    
	    table.on('tool(clerks)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
	    	  var data = obj.data; //获得当前行数据
	    	  var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
	    	  var tr = obj.tr; //获得当前行 tr 的DOM对象
	    	 
	    	  if(layEvent === 'del'){ //删除
	    	    layer.confirm('真的删除该店员吗', function(index){
	    	      obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
	    	      layer.close(index);
	    	      //向服务端发送删除指令
	    	      var ids=obj.data.id;
	    	      $.ajax({  
	    	            type: "POST",  
	    	            url:"/users",  
	    	            data:{id:ids},// 序列化表单值  
	    	            success: function(res) {
	    	            	if(res.code==2000){
	    	            		top.layer.close(index);
	    	          			top.layer.msg("删除成功");
	    	           			layer.closeAll("iframe");
	    	          	 		//刷新父页面
	    	           			tableIns.reload();
	    	            	}
	    	            
	    	            } ,
	    	 		
	    		 		 error: function(request) {  
	    		             alert("请求错误");
	    		         } 
	    	        }); 
	    	    });
	    	  } else if(layEvent === 'edit'){ //编辑
	    		  var ids=obj.data.id;
	    		  editClerk(ids);
	    	    
	    	  }
	    	});
			
			
			  function editClerk(id){
			        var index = layui.layer.open({
			            title: "编辑店员信息",
			            type: 2,
                        area:['500px','500px'],
			            content: "clerk/clerksEdit.html?id="+id
			        });

			  }
	    
	});
	</script> 
</body>
</html>